﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Controllers.Helpers;
using System.Data;
using WebHoiChoMuaBan.Controllers.Admin;
using WebHoiChoMuaBan.Controllers.Global;
using WebHoiChoMuaBan.Controllers.Utility;
using WebHoiChoMuaBan.Models;
using System.IO;

namespace WebHoiChoMuaBan.Controllers
{
    public class AdminPhotoController : AdminController
    {
        //
        // GET: /Photo/

        public ActionResult Index()
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    PhotoHelper photoHelper = new PhotoHelper();
                    ViewData["ListPhotos"] = photoHelper.getAllPhotos(pageNum,Config.ITEMS_PER_ADMINPAGE);
                    return View("~/Views/Admin/Photo.aspx");
                }
            }
            return HomeAdminPage();
        }

        public ActionResult ListAlbum()
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int pageNum;
                    if (!int.TryParse(Request.QueryString["p"], out pageNum))
                        pageNum = 1;
                    PhotoAlbumHelper albumHelper = new PhotoAlbumHelper();
                    ViewData["ListAlbum"] = albumHelper.getAllAlbum(pageNum, Config.ITEMS_PER_ADMINPAGE);
                    return View("~/Views/Admin/Album.aspx");
                }
            }
            return HomeAdminPage();
        }

        [HttpGet]
        public ActionResult DeletePhoto(int photoId)
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int currPage = 0;
                    currPage = int.Parse(Request.QueryString["p"]);
                    if (currPage == 0)
                        currPage = 1;
                    ImageHelper imgHelper = new ImageHelper();
                    //Xóa các photo thuộc album trên
                    PhotoHelper photoHelper = new PhotoHelper();
                    Photo photo = photoHelper.GetPhotoById(photoId);

                    photoHelper.Delete(photo);
                    imgHelper.DeleteImg(Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), "thumbnail_" + photo.PhotoFileName));
                    imgHelper.DeleteImg(Path.Combine(Server.MapPath("~/Content/Images/Upload"), photo.PhotoFileName));
                    photoHelper.Save();
                    return RedirectToAction("Index", new { p = currPage });
                }
            }
            return HomeAdminPage();
        }

        [HttpGet]
        public ActionResult DeleteAlbum(int albumId)
        {
            if (IsLogin && checkRight())
            {
                if ((bool)ViewData["IsOwner"])
                {
                    int currPage = 0;
                    currPage = int.Parse(Request.QueryString["p"]);
                    if (currPage == 0)
                        currPage = 1;
                    ImageHelper imgHelper = new ImageHelper();
                    //Xóa các photo thuộc album trên
                    PhotoHelper photoHelper = new PhotoHelper();
                    List<Photo> listPhoto = photoHelper.LoadAllPhotosOfAlbum(albumId);
                    foreach (Photo photo in listPhoto)
                    {
                        photoHelper.Delete(photo);
                        imgHelper.DeleteImg(Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), "thumbnail_" + photo.PhotoFileName));
                        imgHelper.DeleteImg(Path.Combine(Server.MapPath("~/Content/Images/Upload"), photo.PhotoFileName));
                        photoHelper.Save();
                    }

                    //Xóa album
                    PhotoAlbumHelper albumHelper = new PhotoAlbumHelper();
                    PhotoAlbum album = albumHelper.GetAlbumById(albumId);
                    imgHelper.DeleteImg(Path.Combine(Server.MapPath("~/Content/Images/Upload/Thumbnail"), "thumbnail_" + album.AlbumThumb));
                    albumHelper.Delete(album);
                    albumHelper.Save();
                    return RedirectToAction("ListAlbum", new { p = currPage });
                }
            }
            return HomeAdminPage();
        }
    }
}
